//
//  KDTitleView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/2.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDTitleView.h"

#define TitleViewHeight (20+20+45)/2
#define ButtonWidth 152/2
#define ButtonHeight 45/2

@implementation KDTitleView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setFrame:(CGRect){0,0,kScreenWidth,TitleViewHeight}];
        self.backView = [[UIView alloc] initWithFrame:self.bounds];
        self.backView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
        [self addSubview:self.backView];
        
        [self  createUI];
    }
    return self;
}
- (void)createUI
{
    self.button1 = [self getButton];
    [_button1 setTitle:@"日程表" forState:UIControlStateNormal];
    [self.backView addSubview:_button1];
    [self buttonClick:_button1];
    
    self.button2 = [self getButton];
    [_button2 setTitle:@"出诊设置" forState:UIControlStateNormal];
    [_button2 setFrame:(CGRect){X(_button1)+WIDTH(_button1),Y(_button1),WIDTH(_button1),HEIGHT(_button1)}];
    [self.backView addSubview:_button2];
}

- (void)buttonClick:(UIButton *)btn
{
    if (!btn.selected) {
        btn.selected = !btn.selected;
        [btn setBackgroundColor:kNavBlueColor];
        
        
        if (btn == _button1) {
            [_button2 setBackgroundColor:[UIColor clearColor]];
            [_button2 setSelected:NO];
        }else{
            [_button1 setBackgroundColor:[UIColor clearColor]];
            [_button1 setSelected:NO];
        }
        
        if ([self.clickDelegate respondsToSelector:@selector(buttonClick:)]) {
            [self.clickDelegate buttonClick:btn == _button1 ? 1 : 2];
        }
    }
    
}

- (UIButton *)getButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:(CGRect){(kScreenWidth-ButtonWidth*2)/2,10,ButtonWidth,ButtonHeight}];
//    [button setTitle:@"日程表" forState:UIControlStateNormal];
//    [button.titleLabel setFont:[UIFont systemFontOfSize:32]];
    [button setBackgroundColor:[UIColor clearColor]];
    [button setTitleColor:kNavBlueColor forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.cornerRadius = 0;//设置那个圆角的有多圆
    button.layer.borderWidth = 1;//设置边框的宽度
    button.layer.borderColor = [kNavBlueColor CGColor];
    button.layer.masksToBounds = YES;
    return button;
}

@end
